﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Globalization;
using TheDrinksApp.Database;

namespace TheDrinksApp.Screen.CurrentOrder
{
    public partial class HowMuchScreen : UserControl
    {
        private string price;

        public string Price
        {
            get { return price; }
            set
            {
                price = value;
                if (price.Length == 0)
                    tbPrice.Text = "0";
                else
                    tbPrice.Text = price;
               
            }
        }

        public HowMuchScreen()
        {
            InitializeComponent();
        }

        

        private void UpdatePrice(string i)
        {
            if (i.Equals(".") && Price.Contains("."))
            {
            }
            else
            {
                Price += i;
            }
            if (Price[0] == '0' && Price.Length > 1)
            {
                Price = Price.Substring(1);
            }
            FormatPrice();
        }

        #region NumPad 0-9 click

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            UpdatePrice("1");
        }

        private void btn2_Click(object sender, RoutedEventArgs e)
        {
            UpdatePrice("2");
        }

        private void btn3_Click(object sender, RoutedEventArgs e)
        {
            UpdatePrice("3");
        }

        private void btn4_Click(object sender, RoutedEventArgs e)
        {
            UpdatePrice("4");
        }

        private void btn5_Click(object sender, RoutedEventArgs e)
        {
            UpdatePrice("5");
        }

        private void btn6_Click(object sender, RoutedEventArgs e)
        {
            UpdatePrice("6");
        }

        private void btn7_Click(object sender, RoutedEventArgs e)
        {
            UpdatePrice("7");
        }

        private void btn8_Click(object sender, RoutedEventArgs e)
        {
            UpdatePrice("8");
        }

        private void btn9_Click(object sender, RoutedEventArgs e)
        {
            UpdatePrice("9");
        }

        private void btn0_Click(object sender, RoutedEventArgs e)
        {
            UpdatePrice("0");
        }
        #endregion

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(Price))
            {
                Price = Price.Substring(0, Price.Length - 1);
            }
        }

        private void btnDot_Click(object sender, RoutedEventArgs e)
        {
            if (!tbPrice.Text.Contains("."))
            {
                Price += ".";
            }
        }

        private void btnTotal_Click(object sender, RoutedEventArgs e)
        {
            if (price.Length == 0)
            {
                if (Util.goFromRecentOrCurrent == 0)
                    Util.HowMuch = 0;
                else
                {
                    Util.HowMuchRecent = 0;
                    Util.orderEditting.HowMuch = Util.HowMuchRecent;
                    Util.UpdateOrder(Util.orderEditting);
                }
            }
            else
            {
                if (Util.goFromRecentOrCurrent == 0)
                    Util.HowMuch = (float)Math.Round(float.Parse(price), 2);
                else
                {
                    Util.HowMuchRecent = (float)Math.Round(float.Parse(price), 2);
                    Util.orderEditting.HowMuch = Util.HowMuchRecent;
                    Util.UpdateOrder(Util.orderEditting);
                }
                
            }
            
            GlobalVariable.mainPage.ShowPage(PAGE.SUMARY_SCREEN);
        }

        private void FormatPrice()
        {
            if (Price.Length == 0)
            {
                Price = "0";
            }
            if (Price.StartsWith("."))
            {
                Price = "0" + Price;
            }
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            if (Util.goFromRecentOrCurrent == 0)
            {
                if (Util.HowMuch <= 0)
                {
                    Price = "0";
                }
                else
                {
                    Price = Util.HowMuch.ToString();
                }
            }
            else
            {
                if (Util.HowMuchRecent <= 0)
                {
                    Price = "0";
                }
                else
                {
                    Price= Util.HowMuchRecent.ToString(); ;
                }
            }
        }
    }
}
